<template>
    <view class="App_gray">
        <view class="form_wrap">
            <count-textarea  class="app-wh100" v-model="content" :contentNum="limitNum" :hasMaxLength="false" :height="100" @blur="blur" :showTip="false" :showNum="true" :placeholder="placeholder" />
            <view class="tip">支持{{limitNum}}位中英文、数字</view>
            <view class="confirm_btn_wrap">
                <view class="confirm_btn" @click="confirm">确定</view>
            </view>
        </view>
    </view>
</template>

<script>
import countTextarea from '@/components/AppFiledTextarea'
import { addTemplateSpeech, editTemplateSpeech, editStageName } from '@/api/constructionSite'
export default {
	name: 'editStage',
	components: {
		countTextarea
	},
	data() {
		return {
			content: '',
			option: {},
			nameReg: /^[a-zA-Z0-9\u2E80-\u9FFF]+$/g,
			limitNum: 300,
			placeholder: '请输入模板话术'
		}
	},
	onLoad(option) {
		this.option = option
		if (option.content) {
			this.content = decodeURIComponent(option.content)
		}
		if (option.name) {
			this.limitNum = 2
			this.content = decodeURIComponent(option.name)
		}
		uni.setNavigationBarTitle({
			title: option.name ? '编辑阶段' : option.content ? '编辑模板话术' : '添加模板话术'
		})
	},
	methods: {
		postRequest() {
			const params = {}
			if (this.option.content) {
				params.content = this.content
				params.id = this.option.id
				return editTemplateSpeech(params)
			} else if (this.option.name) {
				params.id = this.option.stage_id
				params.name = this.content
				return editStageName(params)
			}
			params.content = this.content
			params.stage_id = this.option.stage_id
			return addTemplateSpeech(params)
		},
		confirm() {
			if (!this.content) {
				uni.showToast({
					title: '请输入',
					icon: 'none'
				})
				return
			}
			if ([...this.content].length > this.limitNum) {
				uni.showToast({
					title: '字数已超出',
					icon: 'none'
				})
				return
			}
			if (this.option.name) {
				if (!this.nameReg.test(this.content)) {
					uni.showToast({
						title: '请输入合法内容',
						icon: 'none'
					})
					return
				}
			}
			this.postRequest().then(() => {
				uni.showToast({
					title: this.option.content || this.option.name ? '修改成功' : '添加成功',
					icon: 'success'
				})
				setTimeout(() => {
					console.log(this.option.stage_id, 'editStage this.option.stage_id')
					// uni.navigateTo({
					// 	url: `/pages/constructionSite/editTemplateSpeech?ts_id=${this.option.stage_id}`
					// })
					uni.navigateBack()
				}, 500)
			})
		},
		blur() {
			if ([...this.content].length > this.limitNum) {
				this.content = this.content.slice(0, this.limitNum)
			}
		}
	}
}
</script>

<style scoped lang="scss">
.App_gray {
    .form_wrap {
        border-top: 20px solid #f5f5f5;
        .tip {
            font-size: 24px;
            color: #999;
            padding: 20px 20px 0;
        }
        .confirm_btn_wrap {
            padding: 50px 20px 0;
            .confirm_btn {
                height: 80px;
                line-height: 80px;
                text-align: center;
                border-radius: 10px;
                color: #fff;
                font-size: 28px;
                background-color: #FFD11A;
            }
        }
    }
}
</style>
